/*******************************************************************************
 * Copyright (c) 2011 Technische Universität Darmstadt
 * 					  Fachbereich Informatik
 * 					  Theoretische Informatik - Kryptographie und Computeralgebra
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/

package de.tud.cdc;

import java.security.InvalidKeyException;
import java.sql.SQLException;

/**
 * The interface to a database. Every new connector to another database must
 * implement this interface to make the {@link CryptoBird} functional.
 * 
 * @author Franziskus Kiefer
 * 
 */
public interface Database {

	/**
	 * Set initial parameters for the database
	 * 
	 * @param dbPath
	 *            the path to the database as {@link String}
	 * @param password
	 *            the used password as {@link String}
	 * @throws SQLException
	 *             is thrown if the database is in use or the password is wrong
	 * @throws InvalidKeyException 
	 */
	void init(String dbPath, String password) throws SQLException, InvalidKeyException;

	/**
	 * Add a mail to the database
	 * 
	 * @param id
	 *            the identifier (URI) for the message as {@link String}
	 * @param parsed
	 *            the mail body as {@link String}
	 * @param subject
	 * 			  the mail subject as {@link String}
	 */
	void addMail(String id, String parsed, String subject);

	/**
	 * Get the URI where the body contains the search {@link String} "toSearch".
	 * 
	 * @param toSearch the search {@link String}
	 * @param body search in body 
	 * @param subject search in subject
	 * @return an {@link String} array of URIs, where the string was found
	 */
	String[] getMailIdWhere(String toSearch, boolean body, boolean subject);

	/**
	 * Get the URI where the body contains the search {@link String} "toSearch".
	 * Additionally the {@link String} folder has to be part of the URI.
	 * 
	 * @param toSearch
	 *            the search {@link String}
	 * @param folder
	 *            the folder containing the mail as {@link String}
	 * @param body search in body 
	 * @param subject search in subject
	 *
	 * @return an {@link String} array of URIs, where the string was found
	 */
	String[] getMailIdWhereFolder(String toSearch, String folder, boolean body, boolean subject);

	/**
	 * Print the whole database. Only for testing.
	 */
	void viewDB();

	/**
	 * returns the Body for a given URI. Necessary for lightning integration
	 * @param URI the URI of the message set by Thunderbird
	 * @return the Body
	 */
	public String getBodyWhereMailId(String URI);
	
	/**
	 * returns the Subject for a given URI. 
	 * @param URI the URI of the message set by Thunderbird
	 * @return the Body
	 */
	public String getSubjectWhereMailId(String URI);
	
	
	/**
	 * Check whether an Id (URI) exists in the database or not.
	 * 
	 * @param id
	 *            the URI as {@link String}
	 * @return true if it exists, else false
	 */
	boolean checkID(String id);

	/**
	 * Delete the entry with the given ID from the database
	 * 
	 * @param id
	 *            the URI to delete as {@link String}
	 */
	void deleteEntry(String id);

	/**
	 * Change the URI of an entry.
	 * 
	 * @param oldId
	 *            the old URI as {@link String}
	 * @param newId
	 *            the new URI as {@link String}
	 */
	void changeEntry(String oldId, String newId);

	/**
	 * Delete the table.
	 */
	void clear();

	/**
	 * Change multiple entries from the same folder.
	 * 
	 * @param oldUri
	 *            the old base URI as {@link String}
	 * @param newUri
	 *            the new base URI as {@link String}
	 */
	void changeEntryLike(String oldUri, String newUri);

	/**
	 * Delete multiple entries with the given base URI
	 * 
	 * @param uri
	 *            the base URI to delete as {@link String}
	 */
	void deleteEntries(String uri);

	/**
	 * Copy an entry in the database.
	 * 
	 * @param oldUri
	 *            the old URI to copy from as {@link String}
	 * @param newUri
	 *            the new URI to copy to as {@link String}
	 */
	void addCopiedEntry(String oldUri, String newUri);

	/**
	 * Copy multiple entries with the same base URI.
	 * 
	 * @param oldUri
	 *            the old base URI to copy from as {@link String}
	 * @param newUri
	 *            the new base URI to copy to as {@link String}
	 */
	void copyEntryLike(String oldUri, String newUri);

	/**
	 * Shuts the db down. Usefull in case you want to remove the database or do
	 * some other stuff with it....
	 */
	void shutDown();
	
	/**
	 * Getter for the path to the database.
	 * 
	 * @return the path to the database as {@link String}.
	 */
	String getDbPath();

}
